﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IsoCompte.Classe;
using ComponentFactory.Krypton.Toolkit;


namespace IsoCompte
{
    public partial class Login : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        private System.Resources.ResourceManager RM = null;
        private CultureInfo EnglishCulture = new CultureInfo("en-US");
        private CultureInfo FrenchCulture = new CultureInfo("fr-FR");

        public Login()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = FrenchCulture;
            InitializeComponent();
            InitDonnees();
            RM = new System.Resources.ResourceManager("IsoCompte.Login", System.Reflection.Assembly.GetExecutingAssembly());
        }

        private void kBtnConnect_Click(object sender, EventArgs e)
        {
            if (validateForm())
                if (!connexionOK())
                    IsoForm.InfoForm("Information", RM.GetString("ErreurUtilisateurInvalide"));
        }

        private void kCMLang_Click(object sender, EventArgs e)
        {
            switch (((ComponentFactory.Krypton.Toolkit.KryptonContextMenuItem)sender).Text)
            {
                case "Français":
                    System.Threading.Thread.CurrentThread.CurrentUICulture = FrenchCulture;
                    break;
                case "English" :
                    System.Threading.Thread.CurrentThread.CurrentUICulture = EnglishCulture;
                    break;
            }
            AfficherText();
        }

        private void AfficherText()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Login));
            this.kGBMain.Values.Heading = resources.GetString("kGBMain.Values.Heading");
            resources.ApplyResources(this.kGBMain, "kGBMain");
            this.helpProvider.SetHelpString(this.kBtnConnect, resources.GetString("kBtnConnect.HelpString"));
            resources.ApplyResources(this.kBtnConnect, "kBtnConnect");
            this.kBtnConnect.Values.Text = resources.GetString("kBtnConnect.Values.Text");
            this.helpProvider.SetHelpString(this.kTxtMdp, resources.GetString("kTxtMdp.HelpString"));
            resources.ApplyResources(this.kTxtMdp, "kTxtMdp");
            this.helpProvider.SetHelpString(this.kTxtLogin, resources.GetString("kTxtLogin.HelpString"));
            resources.ApplyResources(this.kTxtLogin, "kTxtLogin");
            resources.ApplyResources(this.kPMain, "kPMain");
            resources.ApplyResources(this.kLblTop, "kLblTop");
            this.kLblTop.Values.Text = resources.GetString("kLblTop.Values.Text");
            resources.ApplyResources(this, "$this");
        }

        private bool validateForm()
        {
            bool ret = true;

            if (kTxtLogin.Text.Trim() == string.Empty)
            {
                errorProvider.SetError(kTxtLogin, RM.GetString("ErreurChampNonRempli"));
                ret = false;
            }
            else
                errorProvider.SetError(kTxtLogin, string.Empty);

            if (kTxtMdp.Text.Trim() == string.Empty)
            {
                errorProvider.SetError(kTxtMdp, RM.GetString("ErreurChampNonRempli"));
                ret = false;
            }
            else
                errorProvider.SetError(kTxtMdp, string.Empty);

            return ret;
        }

        private bool connexionOK()
        {
            bool ret = true;
            try
            {
                Utilisateur u = UtilisateurRepository.GetByLogin(kTxtLogin.Text.Trim());
                if (u.MotDePasse != kTxtMdp.Text)
                    ret = false;
            }
            catch (InvalidOperationException ex)
            {
                ret = false;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return ret;
        }

        private void InitDonnees()
        {
            Utilisateur u = new Utilisateur();
            u.Login = "Cyril";
            u.MotDePasse = "cyril";
            UtilisateurRepository.Add(u);
        }

        private void kTxtMdp_Leave(object sender, EventArgs e)
        {
            if (kTxtMdp.Text.Trim() == string.Empty)
                errorProvider.SetError(kTxtMdp, RM.GetString("ErreurChampNonRempli"));

            else
                errorProvider.SetError(kTxtMdp, string.Empty);
        }

        private void kTxtLogin_Leave(object sender, EventArgs e)
        {
            if (kTxtLogin.Text.Trim() == string.Empty)
                errorProvider.SetError(kTxtLogin, RM.GetString("ErreurChampNonRempli"));

            else
                errorProvider.SetError(kTxtLogin, string.Empty);
        }
    }
}
